package cn.lg.soar.system.general.controller;

import cn.lg.soar.common.model.*;
import cn.lg.soar.common.util.current.UserContext;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.core.annotations.constraints.Id;
import cn.lg.soar.database.base.controller.BaseController;
import cn.lg.soar.database.query.ConditionEnum;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.mvc.annotations.*;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.auth.entity.User;
import cn.lg.soar.system.general.entity.UserTag;
import cn.lg.soar.system.general.service.IUserTagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Validated
@Api(tags = "用户管理")
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/user-tag")
public class UserTagController extends BaseController<UserTag, IUserTagService> {

    @Override
    protected Serializable getId(UserTag entity) {
        return entity.getId();
    }

    private final static String SUBJECT = "用户标签";

    @Log(value = "新增" + SUBJECT, data = "#p0", dataId = "#$r.data")
    @IdempotentByParam("#p0")
    @CreateMapping
    public HttpResult<Serializable> create(@RequestBody UserTag data) {
        data.setUserId(UserContext.getUserId());
        return createResult(data);
    }

    @Log(value = "修改" + SUBJECT, data = "#p0", dataId = "#p0.id")
    @UpdateMapping
    public HttpResult<Void> update(@RequestBody @Validated UserTag data) {
        data.setUserId(null);
        return super.updateResult(data);
    }

    @Log(value = "删除" + SUBJECT, data = "#p0", type = Log.Type.TEXT)
    @RemoveMapping
    public HttpResult<Void> remove(@RequestBody List<Long> ids) {
        return super.removeResult(ids);
    }

    @QueryMapping
    public HttpResult<PageResult<UserTag>> query(PageQuery pageQuery, QueryParams params){
        params.addFilter(UserTag::getUserId, ConditionEnum.eq, UserContext.getUserId());
        return queryResult(pageQuery, params);
    }

    @ApiOperation(value = "标签用户")
    @GetMapping("users")
    public HttpResult<List<User>> users(@Id Long tagId){
        return HttpResult.data(service.users(tagId));
    }

    @ApiOperation(value = "用户标签")
    @GetMapping("tags")
    public HttpResult<List<UserTag>> tags(@Id Long userId){
        return HttpResult.data(service.tags(userId));
    }

    @ApiOperation("查询标签用户")
    @GetMapping(value = "query-user")
    public HttpResult<PageResult<User>> queryUser(@RequestParam Long tagId, PageQuery query, User user){
        return HttpResult.data(
                service.queryUser(tagId, query, user)
        );
    }

    @Log(value = "添加标签用户", data = "#p0")
    @ApiOperation("添加标签用户")
    @PostMapping(value = "add-user")
    public HttpResult<Void> addUser(@RequestBody OneToManyDTO<Long, Long> dto){
        service.addUser(dto);
        return HttpResult.success();
    }

    @Log(value = "删除标签用户", data = "#p0", type = Log.Type.TEXT)
    @ApiOperation("删除标签用户")
    @PostMapping(value = "remove-user")
    public HttpResult<Void> removeUser(@RequestBody List<Long> ids){
        service.removeUser(ids);
        return HttpResult.success();
    }

}
